package com.hsxxherp.ation.common.error;

import com.google.common.base.Objects;
import com.hsxxherp.ation.common.utils.json.AtionGsonBuilder;

import java.io.Serializable;

/**
 * 错误日志，内部服务使用
 * <p>
 * 这里需要考虑下rid的生成，后续将实现rid的生成，先暂用@Builder生成构建方法，rid采用雪花算法计算
 */
public class AtionError implements Serializable {


    private static final long serialVersionUID = 1L;

    /**
     * 错误代码
     */
    private String errorCode;

    /**
     * 错误消息
     */
    private String errorMessage;

    /**
     * 错误详情，openApi，调用使用接口时使用
     */
    private String details;

    /**
     * 错误堆栈记录,日志平台使用
     */
    private String stacktrace;

    /**
     * 错误记录追踪码，日志平台使用，用于后期追踪错误信息使用
     */
    private String rid;

    // Builder
    public static Builder builder() {
        return new Builder();
    }

    // getter
    public String getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public String getDetails() {
        return details;
    }

    public String getStacktrace() {
        return stacktrace;
    }

    public String getRid() {
        return rid;
    }

    public String toString() {
        return AtionGsonBuilder.create().toJson(this);
    }

    public static class Builder {
        private AtionError ationError;

        public Builder() {
            this.ationError = new AtionError();
        }

        public Builder errorCode(String errorCode) {
            this.ationError.errorCode = errorCode;
            return this;
        }

        public Builder errorMessage(String errorMessage) {
            this.ationError.errorMessage = errorMessage;
            return this;
        }

        public Builder details(String details) {
            this.ationError.details = details;
            return this;
        }

        public Builder stacktrace(String stacktrace) {
            this.ationError.stacktrace = stacktrace;
            return this;
        }

        public Builder rid(String rid) {
            this.ationError.rid = rid;
            return this;
        }

        public AtionError build() {
            return this.ationError;
        }
    }
}
